module.exports = function ($resource, $state, $http, $q, toaster, $modal) {

	return {

		restrict: 'AE',
		template: require('../views/p_tickettypeOne.html'),
		replace: true,
		scope: {
			'saleobj': '=',
			'util': '=',
			// 'open' : '=',
		},
		link: function (scope, elements, attrs) {
			scope.page = {
				'today': scope.util.date2str(new Date()), //当天时间,不可更改,备用
				'isLoading': false, 	//票种保存按钮置灰，防止连点
				'viewlist': [],			//景区下拉列表
				'buy_effective_dayarr': [
					{'label': '不限', 'value': 9999}
				]
			}
			for (var i = 1; i < 61; i++) {
				scope.page.buy_effective_dayarr.push({'label': i + '天', 'value': i});
			}
			scope.date1 = {
				'label': scope.page.today,
				'opened': false
			}
			scope.date2 = {
				'label': scope.page.today.substring(0, 5) + '12-31',
				'opened': false
			}
			scope.tickettypeobj = {
				'name': scope.saleobj.t_name,
				'view_code': '',
				'ticket_type_code': '',
				'ticket_type_attr': '',
				'sheets_number': 1,
				'periodstart': scope.date1.label,
				'periodend': scope.date1.label,
				'buy_effective_day': 9999,
				'appoint': '0'
			};

			var beforedata = {
				//景区列表
				'viewlist':
					$http({
						'method': 'GET',
						'url': '/api/as/tc/placeview/getCreatePlace1List',
					}),
				//票种属性----页面和js都没用到，暂时注释
				// 'attrlist':
				// 	$http({
				// 		'method': 'GET',
				// 		'url': '/api/as/tc/attr/list',
				// 	}),
			};

			scope.load = function () {
				$q.all(beforedata).then(function (res) {
					if (res.viewlist.data.errcode === 0) {
						scope.page.viewlist = res.viewlist.data.data;
					} else {
						toaster.error({ title: "", body: res.viewlist.data.errmsg });
						return;
					}
					// if (res.attrlist.data.errcode === 0) {
					// 	scope.page.attrlist = res.attrlist.data.data;
					// 	scope.tickettypeobj.ticket_type_attr = res.attrlist.data.data[0].ticket_attr_id;
					// } else {
					// 	toaster.error({ title: "", body: res.attrlist.data.errmsg });
					// 	return;
					// }
				});

				$resource('/api/as/tc/salettype/list', {}, {})
				  .save({'sale_code': scope.saleobj.code}, function(resultData){
					if (resultData.errcode !== 0) {
						toaster.error({ title: "", body: resultData.errmsg });
						return;
					}
					if (resultData.data.length === 1) {
						scope.tickettypeobj = resultData.data[0];
						scope.date1.label = scope.tickettypeobj.periodstart;
						scope.date2.label = scope.tickettypeobj.periodend;
					} else if (resultData.data.length > 1) {
						toaster.error({ title: "", body: '数据错误,票种数大于一条' });
					}
					scope.page.isLoading = false;
				});
			}
			scope.load();

			scope.save = function () {
				scope.page.isLoading = true;
				if (!scope.tickettypeobj.view_code) {
					toaster.error({ title: "", body: '请选择一个景区' });
					return;
				}
				if (!scope.tickettypeobj.name) {
					toaster.error({ title: "", body: '请填写票种名称' });
					return;
				}
				if (!scope.tickettypeobj.sheets_number || scope.tickettypeobj.sheets_number < 1) {
					toaster.error({ title: "", body: '可销次数填写错误' });
					return;
				}

				var para = {
					'id': scope.tickettypeobj.id,
					'name': scope.tickettypeobj.name,
					'sale_code': scope.saleobj.code,
					'periodstart': scope.util.date2str(scope.date1.label),
					'periodend': scope.util.date2str(scope.date2.label),
					'place_code': scope.tickettypeobj.view_code,
					'appoint': scope.tickettypeobj.appoint,
					'sheets_number': scope.tickettypeobj.sheets_number,
					'buy_effective_day': scope.tickettypeobj.buy_effective_day,
					'price': 0,
					'period_scope': scope.tickettypeobj.period_scope
				}
				
				$resource('/api/as/tc/salettype/save', {}, {}).save(para, function (res) {
					if (res.errcode !== 0) {
						toaster.error({ title: "", body: res.errmsg });
						return;
					}
					scope.load();
					toaster.success({ title: "提示", body: '成功' });
					
				});
			};

			//添加有效期范围（时间段）
			scope.addTime = function (obj) {
        var toaster1 = toaster;
				var modalInstance = $modal.open({
					template: require('../views/addTime.html'),
					controller: 'addTime',
					size: 'lg',
					resolve: {
						data: function () {
							return scope.date = {
								'label': scope.page.today,
								'opened': false
							}
						},
						'toaster': function () {
							return toaster1;
						},
						time: function () {
							return obj.period_scope;
						}
					}
				});
				//关闭模态框刷新页面
				modalInstance.result.then(function (result) {
					obj.period_scope = result.toString();
				});
			}
		
			scope.dateOpen = function (obj, $event) {
				$event.preventDefault();
				$event.stopPropagation();
				obj.opened = true;
			};
		
		}
	};
};

